﻿using Spectre.Console.Cli;

namespace LoadTestToolbox;

public sealed class TypeResolver : ITypeResolver, IDisposable
{
	private readonly IServiceProvider _provider;

	public TypeResolver(IServiceProvider provider)
		=> _provider = provider ?? throw new ArgumentNullException(nameof(provider));

	public object? Resolve(Type? type)
		=> type != null
			? _provider.GetService(type)
			: null;

	public void Dispose()
	{
		if (_provider is IDisposable disposable)
		{
			disposable.Dispose();
		}
	}
}